<?php
	/**
	 * Ham lay thong tin ca nhan
	 * @id int - User id
	 * retrurn array - User information
	 * */
	function getUserSalary($id){
		$sql = "SELECT * FROM mod_hrm WHERE adminid = '".$id."'";
		$rs = mysql_query($sql);
		$rows = mysql_fetch_array($rs);
		return $rows;
	}
	
	function isFullAdmin($uid){
		$query = "SELECT permid FROM tbladmins JOIN tbladminperms ON tbladmins.roleid = tbladminperms.roleid 
					WHERE permid=64 AND tbladmins.id=". intval($_SESSION["adminid"]);
		$rs = mysql_query($query);
		$rows = mysql_num_rows($rs);
			
		return $full_admin = (!$row)?1:0;
	}
	
	/**
	 * Ham gen ra cac Ca Truc trong thang.
	 * @shift int  -  Ca truc, co tat ca 4 ca: [1-4]
	 * @isWeekends int  - default 0,  neu la ngay cuoi tuan, thi bat len thanh 1
	 * @isHolidays int - default 0, neu la le, thi bat len la 1
	 * @return: lists of checkboxs
	 * */
	function genCheckOptionsBoxes($shift, $isWeekends=0, $isHolidays=0, $day){
		
		if(!isset($_SESSION['adminid']))
			session_start();
		$isFullAdmin = (isFullAdmin($_SESSION['id']))?"":"DISABLED";		
		
		$num = 1;		
		//$name = array('A', 'A', 'C', 'D');
		$coefficients = array(2.0, 1.0, 1.0, 1.44);
		$coefficients_weekends = array(2.5, 1.5, 1.5, 2.0);
		$coefficients_holidays = array(3, 3, 3, 3);
		
		$cf = getAllShiftCoefficient();
		if($isWeekends==1 && $isHolidays==0){			
			$coefficients = explode(",", $cf[1]);
		}elseif($isWeekends==0 && $isHolidays==0){
			$coefficients = explode(",", $cf[0]);
		}else{
			$coefficients = explode(",", $cf[2]);
		}
		//echo $isWeekendDays." | ";
		
		
		
		/*if($isWeekends==1)
			$coefficients = $coefficients_weekends;
		else if($isHolidays==1)
			$coefficients = $coefficients_holidays;*/				
		
		$users = "<p>";
				
		$day = $day<10?"0".$day:$day;
		$currentMonth = (isset($_GET['month']))?$_GET['month']:date('n');
		$currentYear = (isset($_GET['year']))?$_GET['year']:date('Y');		
		
		while($shift>=$num){
			$checkBoxValue = $coefficients[$num-1] . "_s".$day."_".$num;
			//checkShiftBox($_SESSION['adminid'], $currentMonth, $currentYear, $checkBoxValue);						
			$users .= "<input type='checkbox' id='s".$day."_".$num. 
						"' name='staffShift[]' value='" . $checkBoxValue . "' ". $isFullAdmin .
						" onClick='caculator(this.value,".$currentMonth.",".$currentYear. ")' ".checkShiftBox($_SESSION['adminid'], $currentMonth, $currentYear, $checkBoxValue)." />Shift ".$num."<br/>";					
			$num++;
		}		
		return $users."</p>";		
	}
		
	/**
	 * Ham lay thong tin ca truc cua nhan vien, thang, name
	 * @uid int -  User id
	 * @month int - Month of staff's shift
	 * @year int - Year of month
	 * @return: Object results
	 * */
	 function getStaffShiftByID($uid, $month, $year){
		 $query = "SELECT * FROM mod_shift WHERE userid =".$uid." AND month = ".$month." AND year = ".$year;
		 $rs = mysql_query($query);
		 
		 return $rs;
	 }

	 /**
	  * Ham kiem tra check box da dc check len chua
	  * @uid int - User id
	  * @month int - current month
	  * @year int - current year
	  * @shift float - shift
	  * @return string - checked or null
	  * */
	 function checkShiftBox($uid, $month, $year, $shift){
		$rs = getStaffShiftByID($uid, $month, $year);
		$row = mysql_fetch_array($rs);
		
		$arr = explode(",", $row['records']);
		if(in_array($shift, $arr))
			return  "checked";
		else
			return "";
	 }
	 
	 
	 /**
	  * Ham kiem tra co phai la cac thong tin cua shift moi hay ko?
	  * @uid int - User id
	  * @month int - current month
	  * @year int - current year
	  * @return bool - true or false
	  * */
	 function isNewShift($uid, $month, $year){
		 $query = "SELECT count(*) rows FROM mod_shift WHERE userid =".$uid." AND month = ".$month." AND year = ".$year;
		 $rs = mysql_query($query);
		 $row = mysql_fetch_array($rs);
		 
		 return ($row['rows']==1)?false:true;
	 }
	 
	 function getAddOnSalary($uid, $month, $year){
			$query = "SELECT salarytype, salary FROM mod_hrm WHERE adminid = ".$uid;
			$rs = mysql_query($query);			
			$row = mysql_fetch_array($rs);
			
			$total = filterShift(getUserShift($uid, $month, $year));
			
			getUserShift($uid, $month, $year);
			if($row['salarytype']){
				$salary = $total * $row['salary'];
			}else
				$salary = 0;
			var_dump($salary);
			
			return number_format($salary,0, ",", " ");
	 }
	 
	 /**
	  * Ham lay cac he so cua nguoi dung trong thang, nam chi dinh
	  * @uid int - User id
	  * @month int - Month of shift(records)
	  * @year int - Year of shift(records)
	  * */
	 function getUserShift($uid, $month, $year){
			$query = "SELECT records FROM mod_shift WHERE userid = " . $uid .
							" AND month = " . $month . " AND year =" . $year;
			$rs = mysql_query($query);
			$rows = mysql_fetch_array($rs);
			
			return $memberShifts = explode(",", $rows['records']);			
	 }
	 
	 /**
	  * Ham loc cac he so tu 1 chuoi fuc : 2.5_s01_1/ 2_s29_4
	  * @arrShift string - chuoi phuc
	  * @return float - tong cac he so
	  * */
	 function filterShift($arrShift){
		 $length = count($arrShift);
		 $total = 0.0;
		 foreach ($arrShift as $shift){
			 $total += floatval(getCoefficent($shift));
		 }
		 return $total;
	 }
	 
	 /**
	  * Ham lay he so tu 1 chuoi fuc
	  * @shift string - chuoi phuc
	  * @return float - tong cac he so
	  * */
	 function getCoefficent($shift){
		 $coeff = substr($shift, 0, strlen($shift)-6);
		 return $coeff;
	 }
	 
	 /**
	  * Ham kiem tra ngay hien hanh phai la cuoi tuan khong?
	  * @days int - ngay hien hanh
	  * @return bool - 	true/false
	  * */
	 function isWeekendDays($days){
		 $currentNumDay = date("w");
		 $x = 0;
		 
		 switch($currentNumDay){
			case 1:
				$x = 0;
				break;
			case 2:
				$x = 6;
				break;
			case 3:
				$x = 5;
				break;
			case 4:
				$x = 4;
				break;
			case 5:
				$x = 3;
				break;
			case 6:
				$x = 2;
				break;
			case 0:
				$x = 1;
				break;
		 }
		 
		if(date("w", (time() + (24*60*60)*($days+$x))) == 6 || date("w", (time() + (24*60*60)*($days+$x))) == 0){
			return true;
		}
		else{
			return false;
		}
	 }
	 
	 /**
	  * Ham lay cac he so cua tat ca cac loai ngay nghi
	  * @return array - mang~ cac he so cua cac loai ngay nghi.
	  * */
	 function getAllShiftCoefficient(){
		$query = "SELECT val FROM mod_config WHERE config like 'shift_coefficient_%'";
		$rs = mysql_query($query);
		while($rows = mysql_fetch_array($rs)){
			$shiftCoefficient[] = $rows['val'];
			
		}		
		return $shiftCoefficient;
	 }
	 
	 /**
	  * Ham lay cac ngay nghi trong nam
	  * @year int - Nam chi dinh de lay cac ngay nghi
	  * @return string - chuoi cac ngay nghi
	  * */
	 function getAllHolidaysByYear($year){
		 $query = "SELECT val FROM mod_config WHERE config like '".$year."'";
		 $rs = mysql_query($query);
		 $rows = mysql_fetch_array($rs);
		 
		 return explode(",", $rows['val']);		 
	 }
	 
	 function isHoliday($day, $month, $year){	
		 $arrHolidays = getAllHolidaysByYear($year);
		 $day = $day<10?"0".$day:$day;
		 $month = $month<10?"0".$month:$month;
		 $isHoliday = 0;
		 		 
		 for($i=0, $n=count($arrHolidays); $i<$n; $i++){	
			if(substr($arrHolidays[$i],0,2)==$day && substr($arrHolidays[$i],3,2)==$month){	
				return true;
			}
			else{
				$isHoliday;
			}
		 }
	 }
?>
